//
//  YMFPriceInfoViewController.swift
//  FlyingPigeon
//
//  Created by yang on 2019/5/26.
//  Copyright © 2019年 a. All rights reserved.
//

import Foundation
import UIKit

class YMFPriceCell: UICollectionViewCell {
    var imageView = UIImageView()
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        imageView.contentMode = .scaleToFill
        imageView.frame = contentView.bounds
        contentView.addSubview(imageView)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func updateCellWith(image: UIImage?)
    {
        imageView.image = image
    }
}


class YMFPriceInfoViewController: UIViewController {
    var isSpecialPrice = false  // 是否为特价车界面
    
    var placeModel : YMFPlaceModel?
    
    var collectionView: UICollectionView?
    var dataArray = [YMFPriceModel]()
    
    
//    convenience init(_ isSpecialPrice: Bool = false) {
//       sinit()
//        
//        self.isSpecialPrice = isSpecialPrice
//    }
//    
//    required init?(coder aDecoder: NSCoder) {
//        fatalError("init(coder:) has not been implemented")
//    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        navigationItem.title = "车辆选择"
        
        let layout = UICollectionViewFlowLayout()
        layout.itemSize = CGSize(width: 200, height: 250)
        layout.scrollDirection = .vertical
        
       let collectionView = UICollectionView(frame: CGRect(x: 0, y: 0, width: view.bounds.width, height: view.bounds.height - 64), collectionViewLayout: layout)
        collectionView.backgroundColor = UIColor(red: 245 / 255.0, green: 245 / 255.0, blue: 245 / 255.0, alpha: 1.0)
        collectionView.dataSource = self
        collectionView.delegate = self
        collectionView.register(YMFPriceCell.self, forCellWithReuseIdentifier: "cell")
        view.addSubview(collectionView)
        self.collectionView = collectionView
        
        
        let fileName = isSpecialPrice ? "SpecialPriceCarInfo" :  "carInfo"
        
        if let dataObj = YMFJsonFileManager.getObjectFormFile(fileName: fileName) as? [[String:String]] {
            dataObj.forEach({ (item) in
                let model = YMFPriceModel(dict: item)
                dataArray.append(model)
            })
        }
    }
    
    
    
}


extension YMFPriceInfoViewController: UICollectionViewDataSource, UICollectionViewDelegate {
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return dataArray.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath)
        let model = dataArray[indexPath.row]
        (cell as? YMFPriceCell)?.updateCellWith(image: UIImage(named: model.showImage))
        
        return cell
    }
    
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        let vc = YMFEditOrderViewController(nibName: "YMFEditOrderViewController", bundle: nil)
        vc.model = dataArray[indexPath.row]
        vc.model.placeName = placeModel?.placeName ?? ""
        navigationController?.pushViewController(vc, animated: true)
    }
}
